package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.entity.dto.setmeal.SetmealAddDto;
import com.itheima.entity.dto.setmeal.SetmealUpDto;
import com.itheima.entity.vo.setmeal.SetmealByIdVo;
import com.itheima.entity.vo.setmeal.SetmealPageVo;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author huangzelnag
 * @create 2022-05-11
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 新增套餐
     *
     * @param setmealAddDto
     * @return
     */
    @PostMapping
    public R addSetmeal(@RequestBody SetmealAddDto setmealAddDto) {

        Boolean flag = setmealService.addSetmeal(setmealAddDto);

        Boolean isExist = setmealMapper.selectName(setmealAddDto.getName());
        if (!isExist) {
            return R.error("名字已存在");
        }

        if (flag) {
            return R.success("新增套餐成功");
        }
        return R.error("插入失败");

    }




    /**
     * 分页查询列表套餐信息
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R getSetmealPage(@RequestParam(required = false) String name,
                            @RequestParam(required = false) Integer page,
                            @RequestParam(required = false) Integer pageSize
                            ) {
        SetmealPageVo setmealPage = setmealService.getSetmealPage(name, page, pageSize);

        if (Objects.isNull(setmealPage)) {
            return R.error("获取到的对象为空");
        }
        return R.success(setmealPage);
    }


    /**
     * 根据id查询套餐信息
     *
     * @param setmealId
     * @return
     */
    @GetMapping("/{setmealId}")
    public R getBySetmealId(@PathVariable String setmealId) {

        SetmealByIdVo setmealById = setmealService.getSetmealById(setmealId);


        return R.success(setmealById);

    }


    /**
     * 更新套餐信息
     *
     * @param setmealUpDto
     * @return
     */
    @PutMapping
    public R upSetmeal(@RequestBody SetmealUpDto setmealUpDto) {

        Boolean flag = setmealService.upSetmeal(setmealUpDto);

        if (flag) {
            return R.success("修改成功");
        }

        return R.error("修改失败");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delSetmeal(@RequestParam String ids) {


        String[] idArry = ids.split(",");

        for (String id : idArry) {
              Boolean falg= setmealMapper.selectSetmealStatus( id);
              if(falg){
                  return R.error("套餐正在售卖中，不能删除");
              }
        }

        Boolean aBoolean = setmealService.delSetmeal(idArry);

        if (aBoolean) {
            return R.success("添加成功");
        }
        return R.error("删除失败");
    }


    /**
     * 批量下架上架
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R upSetmealStatus(@PathVariable Integer status,
                             @RequestParam String  ids
                             ){
        String[] idArry = ids.split(",");


         Boolean flag =  setmealService.upSetmealStatus(status,idArry);

        if(flag){
            return R.success("操作成功");
        }

        return R.error("操作失败");


    }









}
